package butines.game;

import java.awt.Graphics2D;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import butines.core.game.Entity;
import butines.core.lqdb.LocalityQueryFacility;
import butines.core.lqdb.ObjectSet;
import butines.core.lqdb.ProximityDatabase;
import butines.game.image.ColorPalleta;

public class World {

	private Map<ColorPalleta, Team> teams = new HashMap<ColorPalleta, Team>();
//	private List<Unit> units = new LinkedList<Unit>();
	
	private LocalityQueryFacility<Entity> database;
	
	public World(float width, float heith) {
		database = new LocalityQueryFacility<Entity>(0, 0, width, heith, 4, 3);
	}
	
	public ProximityDatabase<Entity> getDatabase() {
		return database;
	}

	public ObjectSet<Entity> findEntities(float x, float y, float distance) {
		return (ObjectSet<Entity>)database.findObjects(x, y, distance, null);
	}
	
	
	public Team createTeam(ColorPalleta color) {
		Team team = teams.get(color);
		if (team == null) {
			team = new Team(color);
			teams.put(color, team);
		}
		return team;
	}
		
	public Team getTeam(ColorPalleta color) {
		return teams.get(color);
	}
	
	public Collection<Team> getTeams() {
		return teams.values();
	}
	
//	public List<Unit> getUnits() {
//		if (units == null) {
//			units = new LinkedList<Unit>();
//		}
//		return units;
//	}
//
//	public void setUnits(List<Unit> allUnits) {
//		this.units = allUnits;
//	}
//	
//	public void addUnit(Unit unit) {
//		getUnits().add(unit);
//	}
	
//	public Unit removeUnit(Unit unit) {
//		getUnits().remove(unit);
//		unit.getProximityToken().release();
//		return unit;
//	}
	
	public Entity getEntity(float x, float y) {
		ObjectSet<Entity> os = findEntities(x, y, 16);
		return (os.count() > 0) ? os.get(0) : null;
	}
	
	public List<Entity> getEntities() {
		return (List<Entity>) database.findAllObjects().getObjects();
	}
	
	public void update(float tpf) {
		Iterator<Entity> i = getEntities().iterator();
		while (i.hasNext()) {
			Entity entity = i.next();
			if (entity instanceof Unit) {
				Unit unit = (Unit)entity;
				if (unit.isDead()) {
					unit.release();
					i.remove();
					continue;
				}
			}
			entity.update(tpf);
		}
	}
	
	private Comparator<Entity> ORDER_DRAW = new Comparator<Entity>() {
		public int compare(Entity a, Entity b) {
			return (int)(a.getPosition().y - b.getPosition().y);
		}
	};
	
	public void draw(Graphics2D g) {
		List<Entity> entities = getEntities();
		Collections.sort(entities, ORDER_DRAW);
		for (Entity entity: entities) {
			entity.draw(g);
		}
		for (Entity entity: entities) {
			entity.hud(g);
		}
//		if (Options.showAnnotation) {
//			for (AbstractEntity entity: entities) {
//				entity.annotation(g);
//			}
//			g2d.setAlign(1, 0).setColor(Color.pink);
//			int y = 0;
//			for (Team t: getTeams()) {
//				g2d.print(800, y+=10, "{0} {1} ", t.getUnits().size(), t.getColor());
//			}
//			database.annotation(g2d);
//		}
		
	}
	
}
